﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TsCore;
using System.Data;
using System.Web.Security;

namespace TsWeb.Followers
{
    public partial class UserProfile : System.Web.UI.Page
    {

        public SortDirection SortDirection
        {
            get
            {
                if (ViewState["SortDirection"] == null)
                {
                    ViewState["SortDirection"] = SortDirection.Ascending;
                }
                return (SortDirection)ViewState["SortDirection"];
            }
            set 
            {
                ViewState["SortDirection"] = value;
            }
        }

        [System.Web.Script.Services.ScriptMethod()]
        [System.Web.Services.WebMethod]
        public static List<string> GetTeamNames(string prefixText)
        {
            List<Team> teams = TsSystem.Instance.GetAllTeamsFilteredByName(prefixText);
            List<string> retList = new List<string>();
            foreach (Team t in teams)
                retList.Add(t.Name);
            return retList;
            

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                BindTeams();
        }

        private void BindTeams()
        {
            
            TeamsGrid.DataSource = TsSystem.Instance.GetAllTeamsSortedByName(false);
            TeamsGrid.DataBind();
            TeamsGrid.UseAccessibleHeader = true;
            if (TeamsGrid.HeaderRow != null)
            {
                TeamsGrid.HeaderRow.TableSection = TableRowSection.TableHeader;
            }
            BindFavorites();


        }


        private void BindFavorites()
        {
            GridViewFavorites.DataSource = TsSystem.Instance.GetFullUserData(Membership.GetUser().UserName).Favorites;
            GridViewFavorites.DataBind();
            GridViewFavorites.UseAccessibleHeader = true;
            if (GridViewFavorites.HeaderRow != null)
            {
                GridViewFavorites.HeaderRow.TableSection = TableRowSection.TableHeader;
            }
        }


        protected void TeamsGrid_PageIndexChanged(object sender, EventArgs e)
        {

        }

        protected void TeamsGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //TeamsGrid_Sorting(null, null);
            TeamsGrid.PageIndex = e.NewPageIndex;
            List<Team> SortedTeams = null;
            if (ViewState["SortDirection"] != null)
            {
                if (SortDirection == SortDirection.Ascending)
                {
                    SortedTeams = TsSystem.Instance.GetAllTeamsSortedByName(true);

                }
                else
                {
                    SortedTeams = TsSystem.Instance.GetAllTeamsSortedByName(false);

                }
            }
            else
            {
                SortedTeams = TsSystem.Instance.GetAllTeamsSortedByName(false);
            }
            TeamsGrid.DataSource = SortedTeams;
            TeamsGrid.DataBind();
            
        }

        protected void TeamsGrid_Sorting(object sender, GridViewSortEventArgs e)
        {
            List<Team> SortedTeams = null;
            if (SortDirection == SortDirection.Descending)
            {
                SortedTeams = TsSystem.Instance.GetAllTeamsSortedByName(true);
                SortDirection = SortDirection.Ascending;
            }
            else
            {
                SortedTeams = TsSystem.Instance.GetAllTeamsSortedByName(false);
                SortDirection = SortDirection.Descending;
            }
            TeamsGrid.DataSource = SortedTeams;
            TeamsGrid.DataBind();

        }

        protected void FilterButton_Click(object sender, EventArgs e)
        {
            TeamsGrid.DataSource = TsSystem.Instance.GetAllTeamsFilteredByName(FilterTextBox.Text);
            TeamsGrid.DataBind();

        }

        protected void TeamsGrid_RowCommand(object sender, GridViewCommandEventArgs e)
        {
          
                if (e.CommandName == "DetailCommand")
                {
                    int index = Convert.ToInt32(e.CommandArgument);
                    GridViewRow row = TeamsGrid.Rows[index];
                    int TeamId = Convert.ToInt32(row.Cells[0].Text);
                    Team team = TsSystem.Instance.GetTeam(TeamId);
                    Session["SelectedTeam"] = team;
                    TeamNameTxt.Text = team.Name;
                    TeamSportTxt.Text = team.Sport.Name;
                    ListViewComments.DataSource = team.Comments;
                    ListViewComments.DataBind();
                    TeamLogo.Src = team.Image;
                    ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "call me", "$('#myModal').modal('show');", true);
                    //ModalPopupExtender1.Show();
                }
            
        }

        protected void UserButton_Click(object sender, EventArgs e)
        {
            Team UpdatedTeam = (Team)Session["SelectedTeam"];
            TsSystem.Instance.FollowTeam(Membership.GetUser().UserName, UpdatedTeam);
            ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "call m1e", "$('#myModal').modal('hide');", true);
            BindFavorites();
            

        }

        





  
    }
}